package org.example.movies.controller;

import com.alibaba.fastjson.JSONObject;
import org.example.movies.service.impl.IndexServiceImpl;
import org.example.movies.service.IndexService;
import org.example.movies.utils.IndexData;
import org.example.movies.utils.ResponseEntity;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/19 10:52
 */
@WebServlet("/index")
public class IndexController extends HttpServlet {
    private IndexService service = new IndexServiceImpl();
    public static final String CONTENT_TYPE_JSON = "application/json; charset=utf-8";


    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ResponseEntity<IndexData> responseEntity = new ResponseEntity<IndexData>();
        try {
            IndexData indexData = service.getIndexData();
            responseEntity.setData(indexData);
            responseEntity.setStatus(200);
            responseEntity.setMsg("success");
            resp.setContentType(CONTENT_TYPE_JSON);
        } catch (Exception ex) {
            responseEntity.setMsg("系统繁忙! 请稍后再试!");
        }
        resp.getWriter().println(JSONObject.toJSONString(responseEntity));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
